package cn.edu.cqu.bysj.controllers.sortTrains;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.TimeZone;

import cn.edu.cqu.bysj.domains.QueryLeftNewDTO;

public class ArriveTimeSort implements SortStrategy {

	@Override
	public List<QueryLeftNewDTO> sort(List<QueryLeftNewDTO> trains) {
		class ArriveTimeCompartor implements Comparator<QueryLeftNewDTO>
		{
			@Override
			public int compare(QueryLeftNewDTO o1, QueryLeftNewDTO o2)
			{
				SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
				dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
				try
				{
					return dateFormat.parse(o1.getArriveTime()).getTime() - dateFormat.parse(o2.getArriveTime()).getTime() >= 0 ? 1: -1;
				} catch (ParseException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
					return 0;
				}
			}
		};
		ArriveTimeCompartor arriveTimeCompartor = new ArriveTimeCompartor();
		Collections.sort(trains,arriveTimeCompartor);
		return trains;
	}

}
